﻿Public Class frmRestore

    Private Sub cmdChonDuongDan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdChonDuongDan.Click
        Try
            FBD.ShowDialog()
            txtPath.Text = FBD.FileName
        Catch ex As Exception

        End Try
    End Sub

    Private Sub txtPath_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPath.KeyPress
        Try
            e.Handled = True
        Catch ex As Exception

        End Try
    End Sub

    Private Sub cmdBackup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBackup.Click
        Try
            Dim query_restore As String = ""
            Dim query_path As String = ""
            Dim pathDatabase As String = ""
            Dim randomcls As New Random
            'check path folder is exits
            'If System.IO.File.Exists(txtPath.Text) = False Then
            '    MessageBox.Show("Tập tin không tồn tại.Vui lòng kiểm tra lại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Question)
            '    Exit Sub
            'End If
            
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dbCommand As OleDb.OleDbCommand
            Panel_watting.Visible = True
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            query_path = "SELECT Top 1 [filename] FROM dbo.sysfiles"
            dbCommand = New OleDb.OleDbCommand(query_path, dbConn)
            pathDatabase = dbCommand.ExecuteScalar()
            Dim iPos As Integer = pathDatabase.IndexOf("SonDongNai.mdf")
            pathDatabase = pathDatabase.Substring(0, iPos)
            query_restore = "RESTORE DATABASE [SonDongNai]" & _
                            " FROM  DISK = N'" & txtPath.Text & "'" & _
                            " WITH  FILE = 1, " & _
                            " Move N'SonDongNai' TO N'" & pathDatabase & "SonDongNai.mdf'," & _
                            " Move N'SonDongNai_log' TO N'" & pathDatabase & "SonDongNai_log.ldf', " & _
                            " NOUNLOAD,  STATS = 10"
            dbCommand = New OleDb.OleDbCommand(query_restore, dbConn)
            dbCommand.ExecuteNonQuery()
            dbConn.Close()
            Panel_watting.Visible = False
            MessageBox.Show("Đã thực hiện phục hồi hệ thống thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
End Class